function appendingSignupInfoAsHostel(simpleSignupAns) {
    console.log(simpleSignupAns);
    let selector = ".table-content";
    $(".content").hide();
    $(selector).empty();
    for (let i = 0; i < simpleSignupAns.length; i++) {
        let ans = simpleSignupAns[i].status === 0 ? '未通过' : '通过';
        let a = "<tr><th scope=\"row\">" + i + "</th>\n" +
            "                    <td>" + simpleSignupAns[i].account + "</td>\n" +
            "                    <td>" + simpleSignupAns[i].nickname + "</td>\n" +
            "                    <td>\n" +
            "                        <div id='signupStatus-" + simpleSignupAns[i].signupId + "' data-status='" + simpleSignupAns[i].status + "' data-signup-id='" + simpleSignupAns[i].signupId + "' data-volunteer-id='" + simpleSignupAns[i].userId + "'>\n" +
            "                            <input type=\"submit\"  onclick='acceptTheVolunteer(this)' class=\"btn btn-success accept\" value=\"通过\">\n" +
            "                            <input type=\"submit\" onclick='notAcceptTheVolunteer(this)' class=\"btn btn-warning fail\" value=\"不通过\">\n" +
            "                        </div>\n" +
            "                    </td>\n" +
            "                    <th id='signupId-" + simpleSignupAns[i].signupId + "'>" + ans + "</th></tr>";
        $(selector).append(a);
    }
}

function retrieveSignupInfoAsHostel() {
    $.ajax({
        url: "/signupInfo/retrieveSignupInfoAsHostel",
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
            console.log(response);
            appendingSignupInfoAsHostel(response.data);
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function appendingSignupInfoAsVolunteerWorker(simpleSignupInfos) {
    let selector = ".content";
    $(selector).empty();
    $(".table").hide();
    for (let i = 0; i < simpleSignupInfos.length; i++) {
        let status = simpleSignupInfos[i].status===0?'未通过':'通过';
        let a = "<a href='/items/" + simpleSignupInfos[i].itemId + "' style='color: black'><div class=\"media\">\n" +
            "                    <div class=\"media-left\">\n" +

            "                            <img class=\"media-object\" src=\"" + simpleSignupInfos[i].avatarUrl + "\" alt=\"...\">\n" +

            "                    </div>\n" +
            "                    <div class=\"media-body\">\n" +
            "<span class=\"pull-right time\">\n" +
            "                            <span class=\"\">结束时间：</span>\n" +
            "                            <span class=\"endTime\">" + simpleSignupInfos[i].endTime +
            "</span>\n" +
            "                        </span>\n" +
            "                        <span class=\"pull-right time\">\n" +
            "                            <span class=\"\">开始时间：</span>\n" +
            "                            <span class=\"startTime\">" + simpleSignupInfos[i].startTime + "</span>\n" +
            "                        </span>" +
            "                        <h4 class=\"media-heading\">" + simpleSignupInfos[i].hostelName +"["+ status +"]"+ "</h4>\n" +
            "                        <p>" + simpleSignupInfos[i].introduction + "</p>\n" +
            "                    </div>\n" +
            "                </div></a>";
        $(selector).append(a);
    }
}

function retrieveSignupInfoAsVolunteerWorker() {
    $.ajax({
        url: "/signupInfo/retrieveSignupInfoAsVolunteerWorker",
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
            console.log(response);
            appendingSignupInfoAsVolunteerWorker(response.data);
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function retrieveSignupInfo(role) {
    if (role === 2) {
        retrieveSignupInfoAsHostel()
    } else if (role === 1) {
        retrieveSignupInfoAsVolunteerWorker();
    }
}

function authenticateSignupInfo() {
    $.ajax({
        url: "/authentication_",
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
            if (response.data !== null) {
                $(".authTab").hide();
                $(".user").show();
                $(".publish").show();
                $(".avatarUrl").attr("src", response.data.avatarUrl);
                $(".role").attr("value", response.data.role.id);
                if (response.data.role.id === 2) {
                    $(".recruitTab").show();
                }
                retrieveSignupInfo(response.data.role.id);
            }
            console.log(response);
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function acceptTheVolunteer(e) {
    let signupId = $(e).parent().attr("data-signup-id");
    let status = $(e).parent().attr("data-status");
    if (parseInt(status) === 1) {
        return;
    }
    $.ajax({
        url: "/signupInfo/accept/" + signupId,
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
            console.log(response);
            let selector = "#signupId-" + signupId;
            let selectorStatus = "#signupStatus-" + signupId;
            $(selector).text("通过");
            $(selectorStatus).attr("data-status", 1);
        },
        error: function (error) {
            console.log(error);
        }

    });
}

function notAcceptTheVolunteer(e) {
    let signupId = $(e).parent().attr("data-signup-id");
    let status = $(e).parent().attr("data-status");
    if (parseInt(status) === 0) {
        return;
    }
    $.ajax({
        url: "/signupInfo/notAccept/" + signupId,
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
            // alert("修改成功！");
            let selector = "#signupId-" + signupId;
            let selectorStatus = "#signupStatus-" + signupId;
            $(selector).text("未通过");
            $(selectorStatus).attr("data-status", 0);
        },
        error: function (error) {
            console.log(error);
        }

    });
}